package com.yuqian.talkservers;



import com.google.gson.Gson;
import com.yuqian.bean.Group;
import com.yuqian.bean.Message;
import com.yuqian.bean.TalkingUser;

import java.util.List;
import java.util.TreeMap;

/**
 * Created by Administrator on 2017/7/27 0027.
 */
public class GroupUtils {
    /**
     * 此类主要包含几项内容
     * 1.用户上下线对群的通知
     * 2.群消息的转发
     *
     */

    static Gson gson=new Gson();
    public static TreeMap<String, TreeMap<String,TalkingUser>> groupServerList;

    /**
     * 用户ID 传入用户ID，传出所有的群列表，此处仅传入群列表，而非群成员列表
     * 传入all，则传出所有的群列表
     * ********************************修改****************************
     * 此处不获取个别用户的群列表
     */
    public static void initGroup() {
        List<Group> grouplist;//获取到的群列表，包含群名称和群Id
      //  grouplist = GroupRest.getGrpoup("all");
        groupServerList = new TreeMap<>();
       // for (Group group : grouplist) {
        //    TreeMap<String,TalkingUser> users = new TreeMap<>();
        //    groupServerList.put(group.getId(), users);
      //  }
    }



    /**
     *在群中发送一条消息通过遍历其中的连接向各个客户机发送
     * @param message 消息内容
     * @return
     */
    public static boolean sendMessage(Message message){
        try{
            for (String key : groupServerList.get(message.getTowhere()).keySet()) {
                TalkingUser  user=groupServerList.get(message.getTowhere()).get(key);
                if (user.isDevice1Online()) {
                    try{
                        user.getDevice1().write(gson.toJson(message).toString());
                        user.getDevice1().flush();
                        return true;
                    }catch (Exception e){
                        user.setDevice1Online(false);
                        return false;
                    }

                }else {

                }
                if (user.isDevice2Online()) {
                    try{
                        user.getDevice2().write(gson.toJson(message).toString());
                        user.getDevice2().flush();
                    }catch (Exception e){
                        user.setDevice2Online(false);
                    }

                }else {

                }
            }
        }catch (Exception e){
            return false;
        }
        return true;
    }


    /**
     *
     * @param user 传入一个上线或者下线的用户对象，获取广播内容
     * @return
     */
    public static void getIsOnlineMessage(TalkingUser user) {
        if (!user.isDevice1Online()) {
            if (!user.isDevice2Online()) {
                //编写消息内容，并发送
            }
                sendMessage(new Message());
                sendMessage(new Message());
        } else {
            if (!user.isDevice2Online()) {
                sendMessage(new Message());
            } else {
                sendMessage(new Message());
            }
        }

    }

public static class Test{

        int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    String name;
    }
   public static TreeMap<String,Test> testList;
    public static void main(String[] args) {
        Test test=new Test();
        test.setId(1);
        test.setName("傻子");
        add(test);
        testList.get("1").setName("笨蛋");
        System.out.println( testList.get("1").getName());

    }

    public static void add(Test test){
       testList=new TreeMap<>();
        testList.put("1",test);

    }

}
